#!/bin/sh

set -e

. $CETIC_6LBR_CONF
. $1/6lbr-functions

config_default

MODE_6LBR=$2
DEV=$3
OS=`uname`

if [ "$BRIDGE" != "1" ]; then
	exit
fi

if [ $MODE_6LBR != "tap" ]; then
	exit
fi

case $OS in
	Linux)
		if [ "$CREATE_BRIDGE" = "6LBR" -o "$CREATE_BRIDGE" = "1" ]; then
			echo "6lbr-ifdown: Delete bridge $DEV_BRIDGE"
			ip link set $DEV_BRIDGE down
			ip addr flush dev $DEV_BRIDGE
			ip addr flush dev $DEV
			brctl delbr $DEV_BRIDGE
		else
			echo "6lbr-ifdown: Remove interface $DEV"
			brctl delif $DEV_BRIDGE $DEV
		fi
		;;
	Darwin)
		if [ "$CREATE_BRIDGE" = "6LBR" -o "$CREATE_BRIDGE" = "1" ]; then
			echo "6lbr-ifdown: Delete bridge $DEV_BRIDGE"
			ifconfig $DEV_BRIDGE destroy
		else
			echo "6lbr-ifdown: Remove interface $DEV"
			ifconfig $DEV_BRIDGE delm $DEV
		fi
		;;
	*)
		echo Unknown OS
esac
